/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.object;

import net.worlds.WorldsException;
import net.worlds.Main;
import net.worlds.PropChangeHandler;


public class SpawnPoint extends GameObject{
    private Part parent;
    public SpawnPoint() throws WorldsException
    {
        super("SpawnPoint");
        addPropChangeHandler("Parent", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                onParentChanged((GameObject) oldValue, (GameObject) value);
                if(inWorld)
                {
                    if(value instanceof Part && value != null)
                        rebuildSpawn();
                    else if(parent != null)
                        removeSpawn();
                }
            }
        });
        //x_;
    }
    private void rebuildSpawn() throws WorldsException
    {
        if(parent != null)
            removeSpawn();
        Part p = ((Part)getParent());
        parent = p;
        ((SpawnManager)world.getService("SpawnManager")).addSpawn(p);
    }
    private void removeSpawn() throws WorldsException
    {
        ((SpawnManager)world.getService("SpawnManager")).removeSpawn(parent);
    }
    public void destructor() throws WorldsException
    {
        super.destructor();
        if(parent != null)
        {
            removeSpawn();
        }
    }
    public void addToWorld() throws WorldsException
    {
        super.addToWorld();
        if(getParent() instanceof Part)
        {
            rebuildSpawn();
        }
    }
}
